package com.xhwl.centiotdata.suppliersystem.mili;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.MiLiConstant;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.mili.DeviceMiLiDTO;
import com.xhwl.centiotdata.pojo.dto.mili.LoginMiLiDTO;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: wuyuhao
 * @createDate: 2022/10/13 10:28
 */
@Service
@Slf4j
public class DeviceMiLiHandler {

    @Autowired
    private GeneralMiLiHandler generalMiLiHandler;

    public List<DeviceMiLiDTO> pullDevice(DeviceIntegrationConfigDTO configDTO) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        List<DeviceMiLiDTO> deviceMiLiDTOList = new ArrayList<>();
        Integer index = 1;
        Integer page = 500;
        Integer total;
        do {
            JSONObject req = new JSONObject();
            req.put("PageIndex", index);
            req.put("CountPrePage", page);
            String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                    configDTO.getIpAndPort(), MiLiConstant.DEVICE_QUERY_URL, req);
            // 解析返回的设备列表数据
            JSONObject res = JSON.parseObject(response);
            JSONObject body = res.getJSONObject("Body");
            JSONArray list = body.getJSONArray("List");
            deviceMiLiDTOList.addAll(list.toJavaList(DeviceMiLiDTO.class));
            total = body.getInteger("Total");
        } while ((page * index++) < total);

        return deviceMiLiDTOList;
    }

    public int getDeviceTotal(DeviceIntegrationConfigDTO configDTO) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.DEVICE_TOTAL_URL, new JSONObject());
        // 解析返回的数据
        JSONObject jsonObject = JSON.parseObject(response);
        JSONObject body = jsonObject.getJSONObject("Body");
        return body.getInteger("Total");
    }

    public List<Device> handlePedestrianDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<DeviceMiLiDTO> deviceMiLiDTOList) {
        List<Device> deviceList = new ArrayList<>();
        for(DeviceMiLiDTO item : deviceMiLiDTOList) {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setThirdDeviceId(item.getDeviceNo());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setOnlineStatus(Integer.valueOf(0).equals(item.getOnline()) ? DeviceOnlineStatusEnum.OFFLINE : DeviceOnlineStatusEnum.ONLINE);
            deviceList.add(device);
        }
        return deviceList;
    }
}
